package com.education.common.exception;

import com.education.common.utils.Result;
import com.education.common.utils.ResultCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class SystemExceptionHandler {
    private static final Logger logger= LoggerFactory.getLogger(SystemExceptionHandler.class);
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result resolveException(Exception e){
        Result result=new  Result();
        result.setResultCode(new ResultCode(ResultCode.FAIL,ResultCode.DEFAULT_FAIL_MESSAGE));
        if (e instanceof BusinessException){
            BusinessException businessException=(BusinessException) e;
            if (businessException.getResultCode()!=null){
                result.setResultCode(businessException.getResultCode());
            }
            logger.error("系统异常！！！");
        }
        return result;
    }
}
